package com.socket.socketAsk;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.net.SocketException;

/**
 * AskServer--bdqnPractice
 *
 * @author: Jovi
 * @createTime: 2017-09-08 10:31
 **/
public class AskServer {

    public static void main(String[] args) {
        try {
            DatagramSocket ds = new DatagramSocket(8800);
            byte[] buf = new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf, buf.length);
            ds.receive(dp);
            String mess = new String(buf, 0, dp.getLength());
            System.out.println("客户端说:"+mess);

            String repay = "你好，我在";
            byte [] replys = repay.getBytes();
            SocketAddress sa = dp.getSocketAddress();//获取响应地址
            DatagramPacket dp2 = new DatagramPacket(replys,replys.length,sa);
            ds.send(dp2);

            ds.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
